﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Script_ExtControl : System.Web.UI.UserControl
{
    private string _root = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        _root = Request.ApplicationPath.Length == 1 ? "" : Request.ApplicationPath;
        foreach (Control con in Page.Controls)
        {

        }
    }
    public string Root
    {
        get
        {
            return _root;
        }
    }

    public bool HasMessage
    {
        get
        {
            return !string.IsNullOrEmpty(_title);
        }
    }

    private string _title;
    public string Title
    {
        get
        {
            return _title;
        }
    }

    private string _msg;
    public string Msg
    {
        get { return _msg; }
    }

    private string _link;
    public string Link
    {
        get { return _link; }
    }
    /// <summary>
    /// 设置页面加载完成后的提示信息
    /// </summary>
    /// <param name="title">提示框的窗口标题</param>
    /// <param name="msg">提示信息</param>
    public void SetPageMessage(string title, string msg)
    {
        _title = title;
        _msg = msg;
        _link = string.Empty;
    }

    /// <summary>
    /// 设置页面加载完成后的提示信息, 并确认后跳转连接
    /// </summary>
    /// <param name="title">The title.</param>
    /// <param name="msg">The MSG.</param>
    /// <param name="afterLink">The after link.</param>
    public void SetPageMessage(string title, string msg, string afterLink)
    {
        SetPageMessage(title, msg);
        _link = afterLink;
    }

    /// <summary>
    /// 设置页面加载完成后的提示信息, UpdatePanel 使用.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="title">The title.</param>
    /// <param name="msg">The MSG.</param>
    public void SetAfterMessage(Page page, string title, string msg)
    {
        System.Web.UI.ScriptManager.RegisterStartupScript(
            page,
            page.GetType(), 
            "",
            " (function() { Ext.Msg.alert('" + title + "', '" + msg + "'); }).defer(200) \n",
            true
            );
    }

    /// <summary>
    /// 设置页面加载完成后的提示信息, UpdatePanel 使用.
    /// </summary>
    /// <param name="title">The title.</param>
    /// <param name="msg">The MSG.</param>
    public void SetAfterMessage(string title, string msg)
    {
        SetAfterMessage(Page, title, msg);
    }


    /// <summary>
    /// 设置页面加载完成后的提示信息, UpdatePanel 使用.
    /// </summary>
    /// <param name="title">The title.</param>
    /// <param name="msg">The MSG.</param>
    public void SetAfterMessage(string title, string msg, string afterLink)
    {
        SetAfterMessage(Page, title, msg, afterLink);
    }

    public void SetAfterMessage(Page page, string title, string msg, string afterLink)
    {
        string sc = @"(function() {{ Ext.Msg.show({{
                       title:'{0}',
                       msg: '{1}',
                       buttons: Ext.Msg.OK,
                       fn: {2},
                       icon: Ext.MessageBox.QUESTION
                    }}); }}).defer(200); ";

        string fn = @"function() {{ location.href = '{0}'; }}";

        System.Web.UI.ScriptManager.RegisterStartupScript(
            page,
            page.GetType(),
            "",
            string.Format(sc, title, msg , string.Format(fn, afterLink)),
            true
            );

//        System.Web.UI.ScriptManager.RegisterStartupScript(
//            page,
//            page.GetType(), 
//            "",
//            @" (function() { 
//                    Ext.Msg.alert('" + title + "', '" + msg + @"', 
//                            function(){ location.href = '" + afterLink + @"'; }
//                    ); 
//                }).defer(200) \n",
//            true
//            );
    }


    /// <summary>
    /// 清除提示信息
    /// </summary>
    public void clearMessage()
    {
        _title = string.Empty;
        _msg = string.Empty;
    }
}
